<?php
class verifyController extends base
{
    public function __construct()
    {
        parent::__construct('default');
    }
    /**
     * 验证群组ID
     */
    public function doverifyGroup()
    {
        $gid = common::request('gid','G','');  // 群组ID
        $num = common::request('num','G','');
        $timestamp = common::request('timestamp','G','');
        $sign = common::request('sign','G','');
        $real = 0;
        if(!empty($gid) && !empty($num) && !empty($timestamp) && !empty($sign) && $sign == md5($gid . "lottery" . $num . "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS"))
        {
            $gInfo = $this->load('group')->getGroupInfo($gid);
            if(!empty($gInfo) && $gInfo['isDismiss'] != 1)
            {
                $real = 1;
            }
        }
        $result = json_encode(compact('gid','real'));
        exit($result);
    }
    /**
     * 验证群组ID与群主昵称
     */
    public function dogroupAndName()
    {
        $gid = common::request('gid','G','');  // 群组ID
        $num = common::request('num','G','');
        $timestamp = common::request('timestamp','G','');
        $sign = common::request('sign','G','');
        $hostUid = 0;


        $serverSign = md5($gid . "lottery" . $num. "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS");
        if(!empty($gid) && !empty($num) && !empty($timestamp) && !empty($sign) && $sign == md5($gid . "lottery" . $num. "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS"))        {
            $gInfo = $this->load('group')->getGroupInfo($gid);
            if(!empty($gInfo) && $gInfo['isDismiss'] != 1)
            {

                if($gInfo['uid'] > 0 )
                {
                    $hostUid = $gInfo['uid'];
                }
            }
        }
        $result = json_encode(compact('gid','hostUid'));
        exit($result);
    }
    /**
     * 获取2015上马活动中群组是否中奖及获取用户信息
     */
    public function doshMarathonWinGroup()
    {
        $gid = common::request('gid','G','');  // 群组ID
        $num = common::request('num','G','');   // 随机数， 签名用
        $timestamp = common::request('timestamp','G','');   // 时间戳 签名用
        $sign = common::request('sign','G','');

        $isWinner = 0;  // 是否获奖
        $userList = array();   // 获奖用户列表

        $failUser = 0;

        if(!empty($gid) && !empty($num) && !empty($timestamp) && !empty($sign) && $sign == md5($gid . "lottery" . $num . "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS"))
        {
            $gInfo = $this->load('group')->getGroupInfo($gid);
            if(!empty($gInfo) && $gInfo['isDismiss'] != 1)
            {
                $members = $this->load('group')->getGroupMemberByDeadline($gid, strtotime("2015-09-28"));
                if(count($members) >= 5)
                {
                    foreach ($members as $uid)
                    {
                        $role = $this->load('group')->getGroupRole($gid, $uid);
                        $userInfo = $this->load('client')->getUserInfo($uid);
                        $milieage = $this->load('group')->getShMarathonMileage($uid, $gid);

                        $userList[] = array(
                                'uid' => $uid,
                                'nickname' => $userInfo['nickname'],
                                'isHost' => $role == 1 ? 1 : 0,
                                'milieage' => $milieage
                            );

                        $milieage < 21.0975 && $failUser = 1;
                    }
                }
                else
                {
                    $failUser = 1;
                }
            }
            else
            {
                $failUser = 1;
            }

            $isWinner = $failUser ? 0 : 1;
        }
        $result = json_encode(compact('isWinner','userList'));
        exit($result);
    }
    /**
     * 马拉松活动中群组跑步里程信息
     */
    public function domarathonGroupData()
    {
        $gid = common::request('gid','G','');  // 群组ID
        $applyTime = common::request('applyTime','G',0);  // 群组报名时间
        $date = common::request('date','G', '');  // 跑步 日期
        $num = common::request('num','G','');   // 随机数， 签名用
        $timestamp = common::request('timestamp','G','');   // 时间戳  签名
        $sign = common::request('sign','G','');
        $actStartTime = common::request('actStartTime','G',''); //活动 开始时间
        $actEndTime = common::request('actEndTime','G','');  //活动 结束时间

        $gid = intval($gid);
        $applyTime = intval($applyTime);
        $timestamp = intval($timestamp);

  
        $testTime = strtotime($actStartTime) - 86400*3;
        // 北马
        $actStartTime = date('Ymd') < $actStartTime ? $testTime : strtotime($actStartTime);  // 活动开始时间 
        $actEndTime = strtotime($actEndTime);    // 活动结束时间 

 
        $groupInfo = array();
        $runData = 0.00;

        // 计算签名
        $serverSign = md5($gid . "lottery" . $applyTime .'marathon' . $num . "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS");

        if(!empty($gid) && !empty($applyTime) && strlen($applyTime) == 10 && preg_match('/\d{4}-\d{1,2}-\d{1,2}/', $date) && !empty($num) && !empty($timestamp) && !empty($sign) && $sign == $serverSign)
        {
            $gInfo = $this->load('group')->getGroupInfo($gid);
            if(!empty($gInfo) && $gInfo['isDismiss'] != 1)
            {
                // 群组相关信息
                $groupInfo['gid'] = $gInfo['gid'];
                $groupInfo['name'] = $gInfo['name'];
                $groupInfo['header'] = $gInfo['header'];

                // 群组跑步数据
                $dateStartTime = strtotime($date);
                $end = $dateStartTime + 86400;

                $start = $dateStartTime;

                if($dateStartTime >= $actStartTime && $dateStartTime < $actEndTime)
                {
                    if($applyTime <= $dateStartTime)
                    {
                        $start = $dateStartTime;
                    }
                    else if($applyTime >= $dateStartTime && $applyTime < $end)
                    {
                        $start = $applyTime;
                    }
                }
 
                if(isset($start))
                {    
                    $dids = $this->db->getCol("select distinct(did) from r_group_run where gid = '{$gid}' and updatetime >= '{$start}' and updatetime < '{$end}' and is_delete = 0 and 180 * mileage <= elapsedtime");

                    if(!empty($dids))
                    {
                        $runData = $this->db->getOne("select sum(mileage) from r_data where did ".$this->load('push')->db_create_in($dids)." and is_delete = 0 and overSpeed = 0");
                    }
                    $runData = sprintf("%.2f", $runData);
                }
                
            }
        }
        $result = json_encode(compact('date','groupInfo','runData'));
        exit($result);
    }
    /**
     * 获取用户信息
     */
    public function douserInfo()
    {
        $uid = common::request('uid','G','', TRUE);  // 用户ID
        $num = common::request('num','G','');   // 随机数， 签名用
        $timestamp = common::request('timestamp','G','');   // 时间戳  签名
        $sign = common::request('sign','G','');

        $uid = intval($uid);
        $timestamp = intval($timestamp);

        $userInfo = array();

        // 计算签名
        $serverSign = md5($uid . "lottery" .  $num . "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS");

        if(!empty($uid) && !empty($num) && !empty($timestamp) && !empty($sign) && $sign == $serverSign)
        {
            $info = $this->load('client')->getUserInfo($uid);
            if($info)
            {
                $userInfo = array('nickname' => $info['nickname'], 'header' => $info['header']);
            }
        }

        $userInfo = json_encode($userInfo);
        exit($userInfo);
    }
    /**
     * 马拉松活动中群组用户跑步信息
     */
    public function domarathonUserData()
    {
        $gid = common::request('gid','G','');  // 群组ID
        $applyTime = common::request('applyTime','G',0);  // 群组报名时间
        $date = common::request('date','G', '');  // 跑步 日期
        $num = common::request('num','G','');   // 随机数， 签名用
        $timestamp = common::request('timestamp','G','');   // 时间戳 签名用
        $sign = common::request('sign','G','');
        $actStartTime = common::request('actStartTime','G',''); //活动 开始时间
        $actEndTime = common::request('actEndTime','G','');  //活动 结束时间
        $userList = array();   // 获奖用户列表
        $msg = ''; // 备注信息

        // 兰州马拉松
        /*
        $actStartTime = strtotime("2015-06-05");  // 活动开始时间 
        $actEndTime = strtotime("2015-06-15");    // 活动结束时间 
        */

        // 北马
        //$actStartTime = date('Ymd') < 20150918 ? strtotime("2015-09-16") : strtotime("2015-09-18");  // 活动开始时间 
        //$actEndTime = strtotime("2015-09-28");    // 活动结束时间 

        $testTime = strtotime($actStartTime) - 86400*3;
        // 北马
        $actStartTime = date('Ymd') < $actStartTime ? $testTime : strtotime($actStartTime);  // 活动开始时间 
        $actEndTime = strtotime($actEndTime);    // 活动结束时间 

        $serverSign = md5($gid . "lottery" . $num . 'marathon' . $applyTime . "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS");

        if(!empty($gid) && !empty($num) && !empty($applyTime) && strlen($applyTime) == 10 && !empty($timestamp) && !empty($sign) && $sign == $serverSign)
        {
            $gInfo = $this->load('group')->getGroupInfo($gid);
            if(!empty($gInfo) && $gInfo['isDismiss'] != 1)
            {
                // 取活动结束前加群的成员列表
                $members = $this->load('group')->getGroupMemberByDeadline($gid, $actEndTime);
                
                // 获取要统计的跑步记录的开始时间
                empty($date) && $date = date("Y-m-d");
                $dateStartTime = strtotime($date);
                $end = $dateStartTime + 86400;

                if($dateStartTime >= $actStartTime && $dateStartTime < $actEndTime)
                {
                    if($applyTime <= $dateStartTime)
                    {
                        $start = $dateStartTime;
                    }
                    else if($applyTime >= $dateStartTime && $applyTime < $end)
                    {
                        $start = $applyTime;
                    }
                }

                if(isset($start))
                {
                    foreach ($members as $uid)
                    {
                        // 获取用户在群组中的角色
                        $role = $_ENV['group']->getGroupRole($gid, $uid);
                        $userInfo = $this->load('client')->getUserInfo($uid);

                        if(empty($userInfo))
                        {
                            continue;
                        }
                        // 用户活动期间总里程统计
                        $totalStart = $applyTime > $actStartTime ? $applyTime : $actStartTime;
                        $total = $_ENV['group']->getMemberRunDataByTime($uid, $gid, $totalStart, $actEndTime);

                        $milieage = $_ENV['group']->getMemberRunDataByTime($uid, $gid, $start, $end);

                        $userList[] = array(
                                'uid' => $uid,
                                'nickname' => $userInfo['nickname'],
                                'header' => $userInfo['header'],
                                'isHost' => $role == 1 ? 1 : 0,
                                'milieage' => $milieage,
                                'total' => $total,
                            );

                    }
                }
            }
            else
            {
                $msg = '群组不存在或已解散';
            }
        }
        $result = json_encode(compact('userList','msg'));
        exit($result);
    }
    /**
     * 获取微信用户信息
     */
    public function dowxUserInfo()
    {
        $openid = common::request('openid','G','', TRUE);  // 用户ID
        $num = common::request('num','G','');   // 随机数， 签名用
        $timestamp = common::request('timestamp','G','');   // 时间戳  签名
        $sign = common::request('sign','G','');

        $timestamp = intval($timestamp);

        $userInfo = array();

        // 计算签名
        $serverSign = md5($openid . "lottery" .  $num . "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS");

        if(!empty($openid) && !empty($num) && !empty($timestamp) && !empty($sign) && $sign == $serverSign)
        {
            $info = $this->load('weixin')->getwxUserInfo($openid);
            
            if(!empty($info))
            {
                unset($info['id']);
            }
        }

        if(!isset($info))
        {
            $info = array();
        }

        $info = json_encode($info);
        exit($info);
    }

    /*
     *八大区活动信息
     *
     */
    public function doareaGroupRunbyDay()
    {      
        $groups = common::request('groups','G','');  // 群组ID ,json
        $timestamp = common::request('timestamp','G','');   // 时间戳 签名用
        $date = common::request('date','G', '');  // 跑步 日期
        $sign = common::request('sign','G','');
        $num = common::request('num','G','');

 
        $searchTime = strtotime($date);  // 查询开始时间 
        $endTime = $searchTime + 86399;  // 查询一天结束时间
        
        $serverSign = md5($groups . "eight" .  $num ."war".$date. "hoopchinairun2015".$timestamp."WmChina3WhoopZYTPz0wzccnWSS");

        $List = array();
          
        if(!empty($groups)&& common::is_json($groups)&& !empty($num) && $date && $searchTime && strlen($searchTime) == 10 && !empty($timestamp) && !empty($sign) && $sign == $serverSign)
        {
            $gids = json_decode($groups, true);

            if(is_array($gids))
            { 

                foreach ($gids as $gid)                   
                    {
                        $runData = $this->load('group')->getGroupInfoByDay($gid,$searchTime,$endTime);
                        $List[] = array(
                                'gid' => $gid,
                                'mileage' => $runData['mileage'],
                                'times' => $runData['times'],
                                'members' => $runData['members']
                                );
                    }
            }
        }
      
        $result = json_encode(compact('List'));
        exit($result);
    }

     /*
     *八大区活动统计人数结果
     *
     */
    public function doareaGroupRunbyDayData()
    {   
         $groups = array('16520','17401','18152','16521','17402','18153','16523','17403','18154','16524','17404','18156','16525','17405','18155','16527','17407','18159','16528','17406','18157','16529','17408','18158');
        
        $searchTime = strtotime("2015-08-21");  // 活动开始时间 
        $endTime = strtotime("2015-08-31");    // 活动结束时间
        
        $countUid = 0;
      
        $countUid = $this->load('group')->getActivityUids($groups,$searchTime,$endTime);
                     
        $result = $countUid;

        exit($result);
  
        
    }

    /**
     * 贵人鸟活动 - 通过UID和起始时间戳获取对应用户跑步里程
     */
    public function dogetUserMileage()
    {
        $result = array('mileage' => 0);

        $uid = common::request('uid', 'G', '');
        $startTime = common::request('starttime','G', '');
        $timestamp = common::request('timestamp','G', '');
        $sign = common::request('sign','G', '');
        
        $serverSign = md5($uid . 'eight' . $startTime . 'hoopchinairun2015' . $timestamp . 'WmChina3WhoopZYTPz0wzccnWSS');
    
        if($uid && is_numeric($uid) && $timestamp && $sign && $sign == $serverSign)
        {
            $result['mileage'] = $this->load('run')->getUserMileageByUidAndStartTime($uid, $startTime);
        }

        echo json_encode($result);
    }

    /**
     * 爱心跑
     */
    public function dogetUserRunData()
    {
        $result = array('uid' => 0, 'login' => 0, 'haveRunData' => '', 'runData' => '');

        $token = common::request('token', 'P');
        $startTime = common::request('starttime','P');
        $endTime = common::request('endtime','P');
        $timestamp = common::request('timestamp','P');
        $sign = common::request('sign','P');

        $serverSign = md5($token . 'eight' . $startTime . 'hoopchinairun2015' . $timestamp . 'WmChina3WhoopZYTPz0wzccnWSS');

        if($token && $startTime && $endTime && $timestamp && is_numeric($startTime) && is_numeric($endTime) && $sign && $sign == $serverSign)
        {
            $this->vn = '2700';
            $this->token = $token;
            $this->in_user();

            if($this->uid)
            {
                $result['uid'] = $this->uid;
                $result['login'] = 1;

                $result['haveRunData'] = $this->db->getOne("select count(1) from r_data where uid='{$this->uid}' and addtime<{$startTime} and is_delete=0 and overSpeed=0");

                $sql = "select uid,mileage,addtime from r_data where uid='{$this->uid}' and addtime>={$startTime} and addtime<={$endTime} and is_delete=0 and overSpeed=0 order by did desc limit 200";

                $result['runData'] = $this->db->getAll($sql);
            }
        }

        echo json_encode($result);
    }

    //获取用户信息
    public function dogetUserInfo(){
        $phone = common::request('phone','G','',true);
        $pwd = common::request('password','G','',true);
        $sign = common::request('sign','G');
        $time = common::request('time',"G");
        if(empty($phone) || empty($pwd)){
            echo json_encode(['status'=>101,'msg'=>"手机号或密码不能为空！",'data'=>'']);
            exit;
        }
        if($sign == md5($phone . "lottery" . $pwd . "ijoggers2016".$time."WmChina3WhoopZYTPz0wzccnWSS")){
            $userData = $this->db->getRow("select uid,nickname,header from r_user where phone='$phone' and pwd='$pwd'");
            if(empty($userData)){
                echo json_encode(['status'=>102,'msg'=>"手机号或密码不正确！",'data'=>'']);
            }else{
                $time = time();
                $tokenStr = md5($userData['uid'] . $time . $this->config['tokenStr']);
                $uid = $this->alphaId->getHash($userData['uid']);
                $tokenArr = array($uid, $time, $tokenStr);
                $token = implode('-', $tokenArr);
                echo json_encode(['status'=>200,'msg'=>"",'uid'=>$userData['uid'],'data'=>['nickname'=>$userData['nickname'],'header'=>$userData['header'],'token'=>$token]]);
            }
        }else{
            echo json_encode(['status'=>103,'msg'=>"非法访问！",'data'=>'']);
            exit;
        }

    }

}
